using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace My.IdlConverter.Winshell
{
    class Config
    {
        private My.IdlConverter.Core.Config.EnvironmentConfig environmentConfig = new My.IdlConverter.Core.Config.EnvironmentConfig();
        private My.IdlConverter.Sdkmachine.Config.SdkmachineConfig sdkConfig = new My.IdlConverter.Sdkmachine.Config.SdkmachineConfig();

        private static Config instance;
        static Config()
        {
            instance = new Config();
        }

        public static Config Instance
        {
            get { return instance; }
        }

        public My.IdlConverter.Core.Config.EnvironmentConfig EnvironmentConfig
        {
            get { return environmentConfig; }
        }

        public My.IdlConverter.Sdkmachine.Config.SdkmachineConfig SdkConfig
        {
            get { return sdkConfig; }
            set { sdkConfig = value; }
        }

        public static bool ValidIdlConverterPath(string absoluteIdlConverterPath)
        {
            if (!Directory.Exists(absoluteIdlConverterPath))
            {
                return false;
            }

            return true; 
        }

        public static bool ValidDomPath(string absoluteDomPath)
        {
            if (absoluteDomPath.IndexOf("Interface\\Dom", StringComparison.OrdinalIgnoreCase) == -1
                || !Directory.Exists(absoluteDomPath))
            {
                return false;
            }

            return true;
        }

        public static string[] GetDomFiles(string SDKVersion)
        {
            string absoluteDomPath = System.IO.Path.Combine(Config.Instance.EnvironmentConfig.IdlConverterpath, Config.Instance.SdkConfig.DomPath);
            if (!ValidDomPath(absoluteDomPath))
            {
                return new string[] { };
            }

            IEnumerable<string> filesEnumable = Config.Instance.SdkConfig.GetDomFiles(absoluteDomPath, SDKVersion);
            if (filesEnumable == null)
                return new string[] { };
            StringCollection filesCollection = new StringCollection();
            foreach (string nextFile in filesEnumable)
                filesCollection.Add(nextFile);

            string[] filesArray = new string[filesCollection.Count];
            for(int i = 0; i < filesCollection.Count; ++i)
                filesArray[i] = filesCollection[i];

            return filesArray;
        }

        public static string[] GetLibFilePaths(string version)
        {
            if (!ValidIdlConverterPath(Config.Instance.EnvironmentConfig.IdlConverterpath))
                return new string[] { };
            StringCollection libPaths = Config.Instance.sdkConfig.LibPaths[version];
            string[] libFilePaths = new string[libPaths.Count];
            for (int i = 0; i < libPaths.Count; ++i)
            {
                libFilePaths[i] = System.IO.Path.Combine(Config.Instance.EnvironmentConfig.IdlConverterpath, libPaths[i]);
            }

            return libFilePaths;
        }

        public static string[] PublicFilePaths
        {
            get
          {
              if (!ValidIdlConverterPath(Config.Instance.EnvironmentConfig.IdlConverterpath))
                    return new string[] { };

                StringCollection publicPaths = Config.Instance.SdkConfig.PublicFilePaths;
                string[] publicFilePaths = new string[publicPaths.Count];
                for(int i  = 0; i < publicPaths.Count; ++i)
                {
                    publicFilePaths[i] = System.IO.Path.Combine(Config.Instance.EnvironmentConfig.IdlConverterpath, publicPaths[i].Trim());
                }

                return publicFilePaths;
            }
        }

        public static string AbosoluteDomPath
        {
            get
            {
                return System.IO.Path.Combine(Config.Instance.EnvironmentConfig.IdlConverterpath, Config.Instance.SdkConfig.DomPath);
            }
        }
    }
}
